module AdminApi
  class ApplicationController < ActionController::API
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :null_session
    attr_accessor :class, :short_serializer, :complete_serializer
    befor_action :authenticate_admin_user!
    def initializer
      @class = nil
      @short_serializer = nil
      @complete_serializer = nil
      @permit_params = []
    end
    def create
      item = @class.new create_params
      if @complete_serializer
        render json: item, serializer: @complete_serializer
      else
        render json:item
      end
    end
    def index
      if @short_serializer
        render json: @class.all, each_serializer: @short_serializer
      else
        render json: @class.all
      end
    end
    def update
      item = @class.find params[:id]
      if item.update(create_params)
        if @complete_serializer
          render json: item, serializer: @complete_serializer
        else
          render json:item
        end
      else
        render json: item.errors, status: :bad_request
      end
    end
    def destroy
      @class.where(id: params[:id]).destroy_all
      render nothing: true
    end
    private
      def create_params
        params.require(@class.name).permit(@permit_params)
      end
    end
  end
end
